package 多线程初阶.示例测试;

/**
 * 并发和串行的执行速度差异
 */
public class TestDemo2 {
    private static final long count=1000_0000;
    public static void main(String[] args) throws InterruptedException {
        concurrency();
        serial();
    }
    public static void concurrency() throws InterruptedException {
        //并发执行
        long begin=System.nanoTime();//取当前时间
        Thread thread=new Thread(()->{
            for (int i = 0; i < count; i++) {
            }
        });
        thread.start();
        for (int i = 0; i < count; i++) {
        }
        thread.join();
        long end = System.nanoTime();
        double ms = (end - begin) * 1.0 / 1000 / 1000;
        System.out.printf("并发: %f 毫秒%n", ms);
    }
    private static void serial(){
        //串行执行
        long begin=System.nanoTime();
        for (int i = 0; i < count; i++) {
        }
        for (int i = 0; i < count; i++) {
        }
        long end=System.nanoTime();
        double ms = (end - begin) * 1.0 / 1000 / 1000;
        System.out.printf("串行: %f 毫秒%n", ms);
    }
}

